/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package msgclient;

/**
 *
 * @author Данила
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

class Login extends JFrame implements ActionListener {

    static boolean secureCheck;
    String pass;
    static String username;
    JButton SUBMIT;
    JButton Register;
    JPanel panel;
    JLabel userName, password;
    final JTextField text1, text2;
    
    public static String getUsername() {
        return username;
    }

    Login() {
        userName = new JLabel();
        userName.setText("Username:");
        text1 = new JTextField(15);

        password = new JLabel();
        password.setText("Password:");
        text2 = new JPasswordField(15);

        SUBMIT = new JButton("Submit");
        Register = new JButton("Register");

        panel = new JPanel(new GridLayout(3, 1));
        panel.add(userName);
        panel.add(text1);
        panel.add(password);
        panel.add(text2);
        panel.add(SUBMIT);
        panel.add(Register);
        add(panel, BorderLayout.CENTER);
        SUBMIT.addActionListener(this);
        setTitle("LOGIN FORM");
        Register.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("Register")) {
            username = text1.getText();
            pass = text2.getText();
            try {
                MSGClientImpl.server.receiveClientData(username, pass);
                secureCheck = true;
            } catch (RemoteException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            username = text1.getText();
            pass = text2.getText();
            try {
                secureCheck = MSGClientImpl.server.checkClientData(username, pass);
            } catch (RemoteException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (secureCheck) {
            MSGClientImpl.login.dispose();
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        final Chat chat = new Chat();
                        chat.setSize(600, 500);
                        chat.setVisible(true);
                        chat.setDefaultCloseOperation(MSGClientImpl.server.disconnectClient(username));
                        int delay = 1000; //milliseconds
//                        ActionListener taskPerformer = new ActionListener() {
//
//                            public void actionPerformed(ActionEvent evt) {
//                                chat.updateData(username);
//                            }
//                        };
//                        new Timer(delay, taskPerformer).start();
                    } catch (RemoteException ex) {
                        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }
    }
}
